package com.ruoyi.act.module.api;

import com.ruoyi.act.module.service.IDeployService;
import com.ruoyi.act.module.service.IProcessService;
import com.ruoyi.act.module.domain.ActProcess;
import com.ruoyi.act.module.service.ITaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/process")
public class ApiProcessController extends BaseController {

    @Autowired
    private IProcessService iProcessService;
    @Autowired
    private IDeployService iDeployService;
    @Autowired
    private ITaskService iTaskService;

    /**
     * 启动流程实例
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/start")
    public AjaxResult startProcess(@RequestBody ActProcess actProcess) {
        ProcessInstance instance = iProcessService.startProcessInstanceByKey(actProcess);
        if (instance != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", instance.getId());
            map.put("name", instance.getName());
            map.put("deploymentId", instance.getDeploymentId());
            map.put("businessKey", instance.getBusinessKey());
            map.put("desc", instance.getDescription());
            map.put("processDefinitionId", instance.getProcessDefinitionId());
            map.put("processDefinitionKey", instance.getProcessDefinitionKey());
            map.put("processDefinitionName", instance.getProcessDefinitionName());
            List<String> ids = (List<String>) actProcess.getVariables().get("subUserId");
            actProcess.setTaskAssignee(ids.get(0));
            Task task = iTaskService.query(actProcess);
            map.put("taskId",task.getId());
            return AjaxResult.success(map);
        }

        return AjaxResult.error();
    }

    /**
     * 暂停正在运行的流程实例
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/suspend")
    public AjaxResult suspend(@RequestBody ActProcess actProcess) {
        iProcessService.suspendProcessInstanceById(actProcess.getId());
        return AjaxResult.success();
    }

    /**
     * 激活正在运行的流程实例
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/activate")
    public AjaxResult activate(@RequestBody ActProcess actProcess) {
        iProcessService.activateProcessInstanceById(actProcess.getId());
        return AjaxResult.success();
    }

    /**
     * 删除正在运行的流程实例
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult deleteProcessInstance(@RequestBody ActProcess actProcess) {
        String reason = null;
        if (StringUtils.isBlank(actProcess.getReason())) {
            reason = "用户没有给出具体删除原因";
        } else {
            reason = actProcess.getReason();
        }
        iProcessService.deleteProcessInstance(actProcess.getId(), reason);
        return AjaxResult.success();
    }

}
